<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Rule;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login(Request $Request){
        if(!$Request->username){
            return response()->json(['code' => '403', 'message' => '请输入用户名']);
        }
        if(!$Request->password){
            return response()->json(['code' => '403', 'message' => '请输入密码']);
        }

        $data = Admin::where('username',$Request->username)->first();
        if(!$data){
            return response()->json(['code' => '404', 'message' => '账号不存在']);
        }
        $is = Hash::check($Request->password, $data->password);
        if(!$is){
            return response()->json(['code' => '500', 'message' => '密码错误']);
        }
        //判断权限
        $data->token = $data->createToken('admin_api')->accessToken;
        $data->type = 'admin-refresh';
        return response()->json(['code' => 200, 'message' => 'success','result'=>$data]);
    }

    public function index(Request $Request){
        $data = [];
        $data['menus'] = Rule::get();
        $data['siteConfig'] = [];
        $data['terminal'] = [];
        return response()->json(['code' => 200, 'message' => 'success','result'=>$data]);
    }
}
